﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MockupDesigner.Items;

namespace MockupDesigner.AppItems.Commands
{
    public class PropertyChangedCommand : ICommand
    {

        private ItemBase item;

        private string property;
        private object oldValue;
        private object newValue;

        public CommandType CommandType
        {
            get { return CommandType.Undoable; }
        }


        public void Execute(object target, params object[] parameters)
        {
            if (!(target is ItemBase))
            {
                throw new InvalidCastException("The target must be a ItemBase");
            }
            this.item = (ItemBase)target;

            if(!(parameters[0] is string))
            {
                throw new InvalidCastException("The target must be a string");
            }
            this.property = (string)parameters[0];
            this.newValue = parameters[1];
            this.oldValue = parameters[2];

            Redo();
        }

        public void Redo()
        {
            this.item.GetType().GetProperty(this.property).SetValue(this.item, this.newValue, null);
        }

        public void Undo()
        {
            this.item.GetType().GetProperty(this.property).SetValue(this.item, this.oldValue, null);
        }

    }
}
